package com.example.javadesign.factory.simplefactory;

/**
 * @author 陳樂
 * @version 1.0.0
 * @ClassName CourseFactory.java
 * @Description TODO
 * @createTime 2022年08月12日 23:13:00
 */
public class CourseFactory {

    public ICourse create(String name) {
        if ("java".equals(name)) {
            return new JavaCourse();
        } else if ("python".equals(name)) {
            return new PythonCourse();
        } else {
            return null;
        }
    }

    public ICourse createByReflect(String className) {
        if (className == null) {
            return null;
        }
        try {
            return (ICourse) Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ICourse createByClass(Class<? extends ICourse> clazz) {
        if (clazz == null) {
            return null;
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
